<template>
  <Dialog :title="t('stageJudge.select.title')" v-model="dialogVisible" width="90%">
    <div class="mentor-select-section">
      <div class="mentor-select-section__left">
        <StageJudgeSelectTeacher
          ref="teacherListRef"
          :awards-id="awardsId"
          :group-id="majorGroupId"
          :rule-id="ruleId"
        />
      </div>
      <div class="mentor-select-section__operate">
        <div>
          <el-button type="primary" :icon="ArrowRightBold as any" @click="handleAdd" />
        </div>
        <div>
          <el-button type="primary" :icon="ArrowLeftBold as any" @click="handleRemove" />
        </div>
      </div>
      <div class="mentor-select-section__right">
        <StageJudgeSelectCurrent
          ref="currentListRef"
          :awards-id="awardsId"
          :group-id="majorGroupId"
          :rule-id="ruleId"
        />
      </div>
    </div>
  </Dialog>
</template>

<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import StageJudgeSelectCurrent from './StageJudgeSelectCurrent.vue'
import StageJudgeSelectTeacher from './StageJudgeSelectTeacher.vue'
import { ArrowLeftBold, ArrowRightBold } from '@element-plus/icons-vue'

const { t } = useI18n()

const awardsId = ref('')
const majorGroupId = ref()
const ruleId = ref('')

const dialogVisible = ref(false)

const teacherListRef = ref<InstanceType<typeof StageJudgeSelectTeacher>>()
const currentListRef = ref<InstanceType<typeof StageJudgeSelectCurrent>>()

const handleAdd = async () => {
  try {
    // 调左侧组件中的接口 批量写入数据
    await teacherListRef.value?.add()
  } catch {
    return
  }
  // 写入完成 刷新右侧 左侧里面会自己刷新
  currentListRef.value?.getList()
}

const handleRemove = async () => {
  try {
    // 调右侧组件中的接口 批量删除数据
    await currentListRef.value?.remove()
  } catch {
    return
  }

  // 删除完成 刷新左侧 右侧里面会自己刷新
  teacherListRef.value?.getData()
}

const open = (_awardsId: string, _majorGroupId: string | undefined, _ruleId: string) => {
  dialogVisible.value = true
  awardsId.value = _awardsId
  majorGroupId.value = _majorGroupId
  ruleId.value = _ruleId
  nextTick(() => {
    teacherListRef.value?.clean()
    teacherListRef.value?.getData()
    currentListRef.value?.clean()
    currentListRef.value?.getList()
  })
}

defineExpose({
  open
})
</script>

<style scoped lang="scss">
.mentor-select-section {
  width: 100%;
  min-height: 600px;
  display: flex;
  gap: 10px;

  .mentor-select-section__left {
    flex: 1;
  }

  .mentor-select-section__operate {
    width: 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 40px;
  }

  .mentor-select-section__right {
    flex: 1;
  }
}
</style>
